<template>
  <div class="BackOffice">
    <div class="search">
      <span>查询日期：</span>
      <el-date-picker
        v-model="searchDate.month"
        type="month"
        value-format="yyyyMM"
        placeholder="选择月"
      >
      </el-date-picker>
      <span>姓名：</span>
      <el-input
        v-model="searchDate.name"
        placeholder="姓名"
        clearable
      ></el-input>
      <el-button type="primary" @click="searchData">查询</el-button>
      <el-button @click="reset">重置</el-button>
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <div class="content">
      <el-table
        v-if="tableData"
        v-loading="loading"
        max-height="550"
        border
        :data="tableData && tableData.list && tableData.list.list"
        tooltip-effect="dark"
      >
        <el-table-column
          v-for="(t, n) in tableData.fields"
          :key="n"
          :prop="t.fields"
          :label="t.translate"
          :min-width="
            flexColumnWidth(t.translate, t.fields, tableData.list.list)
          "
          align="center"
        />
      </el-table>
      <Empty v-else />
    </div>
    <div class="footer">
      <pagination
        :current-page="searchDate.pageDate.pageNum"
        :page-size="searchDate.pageDate.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div v-if="maskFlag" class="mask">
      <div class="table">
        <div class="flex">
          <div class="title">新增数据</div>
          <i class="el-icon-close" @click="close"></i>
        </div>
        <div class="custom">
          <el-form
            label-position="left"
            label-width="150px"
            ref="ruleForm"
            :rules="rules"
            :model="formData"
          >
            <el-form-item label="日期" prop="month">
              <el-date-picker
                v-model="formData.month"
                type="month"
                placeholder="选择日期"
                value-format="yyyyMM"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item label="出勤天数" prop="attendanceDays">
              <el-input v-model="formData.attendanceDays"></el-input>
            </el-form-item>
            <el-form-item label="基本工资" prop="basicPay">
              <el-input v-model="formData.basicPay"></el-input>
            </el-form-item>
            <el-form-item label="工龄奖" prop="seniorityBonus">
              <el-input v-model="formData.seniorityBonus"></el-input>
            </el-form-item>
            <el-form-item label="满勤" prop="workFullHours">
              <el-input v-model="formData.workFullHours"></el-input>
            </el-form-item>
            <el-form-item label="奖励提成" prop="bonusCommission">
              <el-input v-model="formData.bonusCommission"></el-input>
            </el-form-item>
            <el-form-item label="其他" prop="otherOne">
              <el-input v-model="formData.otherOne"></el-input>
            </el-form-item>
            <el-form-item label="应发工资" prop="wagesPayable">
              <el-input v-model="formData.wagesPayable"></el-input>
            </el-form-item>
            <el-form-item label="社保" prop="socialSecurity">
              <el-input v-model="formData.socialSecurity"></el-input>
            </el-form-item>
            <el-form-item label="其他" prop="otherTwo">
              <el-input v-model="formData.otherTwo"></el-input>
            </el-form-item>
            <el-form-item label="实发工资" prop="netPayroll">
              <el-input v-model="formData.netPayroll"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="formData.remarks"></el-input>
            </el-form-item>
          </el-form>
          <div class="btn">
            <el-button type="primary" round @click="save">保存</el-button>
            <el-button round @click="close">取消</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getBackOfficeList, addBackOfficeList } from "@/api/statisticalCenter";

export default {
  name: "BackOffice",
  data() {
    return {
      month: null,
      loading: true,
      searchDate: {
        month: "",
        name: "",
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      total: 0,
      tableData: null,
      maskFlag: false,
      formData: {
        month: "",
        name: "",
        attendanceDays: "",
        basicPay: "",
        seniorityBonus: "",
        workFullHours: "",
        bonusCommission: "",
        otherOne: "",
        wagesPayable: "",
        socialSecurity: "",
        otherTwo: "",
        netPayroll: "",
        remarks: "",
      },
      rules: {
        month: [{ required: true, message: "必填项" }],
        name: [{ required: true, message: "必填项" }],
        attendanceDays: [{ required: true, message: "必填项" }],
        basicPay: [{ required: true, message: "必填项" }],
        seniorityBonus: [{ required: true, message: "必填项" }],
        workFullHours: [{ required: true, message: "必填项" }],
        bonusCommission: [{ required: true, message: "必填项" }],
        otherOne: [{ required: true, message: "必填项" }],
        wagesPayable: [{ required: true, message: "必填项" }],
        socialSecurity: [{ required: true, message: "必填项" }],
        otherTwo: [{ required: true, message: "必填项" }],
        netPayroll: [{ required: true, message: "必填项" }],
        remarks: [{ required: true, message: "必填项" }],
      },
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    save() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          addBackOfficeList(this.formData).then((res) => {
            if (res.code === 200) {
              this.$message.success("添加成功！");
              this.close();
              this.getTableData();
            }
          });
        }
      });
    },
    close() {
      this.maskFlag = false;
      this.formData = {};
    },
    add() {
      this.maskFlag = true;
    },
    getTableData() {
      this.loading = true;
      getBackOfficeList(this.searchDate).then((res) => {
        this.tableData = res.data;
        this.total = res.data.list.total;
        this.loading = false;
      });
    },
    searchData() {
      this.searchDate.pageDate.pageNum = 1;
      this.searchDate.pageDate.pageSize = 10;
      this.getTableData();
    },
    reset() {
      this.searchDate = {
        month: "",
        name: "",
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
      };
    },
    handleSizeChange(size) {
      this.searchDate.pageDate.pageSize = size;
      this.getTableData();
    },
    handleCurrentChange(current) {
      this.searchDate.pageDate.pageNum = current;
      this.getTableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.BackOffice {
  padding: 20px;
  .search {
    padding-bottom: 20px;
    border-bottom: 1px solid #ccc;
    .el-date-editor {
      margin-right: 20px;
      width: 400px;
    }
    span {
      font-size: 14px;
      color: #606266;
    }
    .el-input {
      margin-right: 20px;
      width: 200px;
    }
  }
  .content {
    margin-top: 20px;
  }
  .footer {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
  .mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    background: rgba($color: #000000, $alpha: 0.2);
    .flex {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
    }
    .title {
      padding-left: 12px;
      border-left: 6px solid skyblue;
      font-size: 24px;
      font-weight: bold;
    }
    i {
      font-size: 28px;
      font-weight: bold;
      cursor: pointer;
    }
    .table {
      margin: 80px auto;
      padding: 30px;
      width: 30%;
      border-radius: 24px;
      background-color: #fff;
      .custom {
        margin-bottom: 20px;
        padding: 0 20px;
        .btn {
          display: flex;
          justify-content: space-around;
          .el-button {
            padding: 12px 36px;
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>
